import { observable, action } from 'mobx';
import isEmpty from 'lodash/isEmpty';
import * as API from '../apis/list';
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

export class ContinnuationProcessStore {

    requestUrl = "/workflow/request/ViewRequestForwardSPA.jsp";
    //切换流程时，是否需要从流程列表中移除当前流程
    needRemoveCurrRequest = false;

    @observable currentIndex;
    @observable requestIds = [];
    @observable counts = [];

    //流程页面是否加载完毕
    @observable requestPageLoaded = true;

    constructor() {
    }

    initContinnuationProcessInfo = (params) => {
        API.getContinnuationProcessInfo({viewScope: "doing", ...params}).then(data => {
            this.currentIndex = data.currentIndex;
            this.counts = data.count;
            this.requestIds = data.requestIds;
            this.showRequestPage(this.requestIds[this.currentIndex]);
        })
    }

    @action('切换下一条流程')
    switchNextRequest = () => {
      this.counts--;
      this.requestIds.splice(this.currentIndex, 1);

      //如果流程都处理完毕，则关闭页面
      if(this.requestIds.length == 0) {
        window.close();
      }
      //切换之后如果超过当前数量范围，切换到第一条
      if(this.currentIndex >= this.requestIds.length) {
        this.currentIndex = 0;
      }
      this.showRequestPage(this.requestIds[this.currentIndex]);
    }

    @action('切换流程')
    switchRequest = (type) => {
      
      //流程页面还未加载完毕， 不能切换
      if(!this.requestPageLoaded) {
        return;
      }
      //上一条
      if(type == 'pre' && this.currentIndex != 0) {
        if(this.needRemoveCurrRequest) {
          this.counts--;
          this.requestIds.splice(this.currentIndex, 1);
        }
        this.currentIndex--;
        this.showRequestPage(this.requestIds[this.currentIndex]);
      } else if(type == 'next' && this.currentIndex != this.counts - 1){
        //下一条
        if(this.needRemoveCurrRequest) {
          this.counts--;
          this.requestIds.splice(this.currentIndex, 1);
        } else {
          this.currentIndex++;
        }
        this.showRequestPage(this.requestIds[this.currentIndex]);
      }

      this.needRemoveCurrRequest = false;
    }

    //显示流程页面
    @action
    showRequestPage = (requestid) => {
        if(this.isListOpen()) {
          this.requestPageLoaded = false;
          document.getElementById('formIframe').contentWindow.location.href = this.requestUrl + "?isContinnuationProcess=true&requestid=" + requestid;
        } else {
          window.location.href = this.requestUrl + "?requestid=" + requestid;
        }
    }

    //页面是否由列表打开
    isListOpen = () => {
        let isListOpen = false;
        try {
          //待办列表打开
          if(window.opener._table.reLoad){
            isListOpen = true;
          }
        } catch (e) {}
        try {
            //门户列表打开
            if(jQuery(window.opener.document).find('#btnWfCenterReload').click) {
              isListOpen = true;
            }
        } catch (e) {}
        return isListOpen;
      }

      @action
      setRequestLoadInfo = (requestname) => {
        this.requestPageLoaded = true;
        //document.title = getLabel(517261, "连续处理") + "-" + requestname;
      }

}